package edu.isquen.core.admin.service;

import java.util.Collection;

import edu.isquen.core.admin.dao.SuperuserDAO;
import edu.isquen.core.admin.model.Superuser;
import edu.isquen.core.base.dao.IBaseDAO;
import edu.isquen.core.base.service.BaseServiceImpl;
import edu.isquen.util.CommUtil;
import edu.isquen.util.factory.DaoFactory;
import edu.isquen.web.exception.BaseException;

public class SuperuserServiceImpl extends BaseServiceImpl implements SuperuserService {

	private static final long serialVersionUID = -7523750654479217539L;

	public Superuser doSaveOrUpdate(Superuser superuser) throws Exception {
		if (superuser == null) {
			return null;
		}
		if(superuser.getLoginpwd()==null){
			superuser.setLoginpwd("");
		}else{
			superuser.setLoginpwd(CommUtil.encodeToMD5(superuser.getLoginpwd()));
		}
		if (!CommUtil.validateString(superuser.getId())) {
			Superuser forExamineUser = new Superuser();
			forExamineUser.setLoginno(superuser.getLoginno());
			if (getDao().findByAny(forExamineUser).getDatas().size() <= 0) {
				this.doCreate(superuser);
			}else{
				throw new BaseException("Name is exit..!");
			}
		} else {
			this.doUpdate(superuser);
		}
		return superuser;
	}

	public Collection<Superuser> doList() {
		// TODO Auto-generated method stub
		return null;
	}

	public Superuser doLogin(String loginname, String loginpwd) throws Exception {
		if (!CommUtil.validateString(loginname)
				|| !CommUtil.validateString(loginpwd)) {
			return null;
		}
		return ((SuperuserDAO) getDao()).login(loginname, CommUtil.encodeToMD5(loginpwd));
	}

	@Override
	public IBaseDAO<?> getDao() throws Exception {
		return DaoFactory.getDao(Superuser.class.getName());
	}

}
